﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using Maticsoft.DBUtility;

namespace ZG_member.DAL
{
    public class Worker
    {
        public bool add(ZG_member.Model.Worker model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into Worker(");
            strSql.Append("WorkerId,Name,Sex,BirthDate,Money)");
            strSql.Append("values (");
            strSql.Append("@WorkerId,@Name,@Sex,@BirthDate,@Money)");
            SqlParameter[] parameters = {
					 
					new SqlParameter("@WorkerId", SqlDbType.VarChar,50),
					new SqlParameter("@Name", SqlDbType.VarChar,50),
					new SqlParameter("@Sex", SqlDbType.VarChar,50),
					new SqlParameter("@BirthDate", SqlDbType.DateTime,200),
                    new SqlParameter("@Money", SqlDbType.Money,200)};

            parameters[0].Value = model.WorkerId;
            parameters[1].Value = model.Name;
            parameters[2].Value = model.Sex;
            parameters[3].Value = model.BirthDate;
            parameters[4].Value = model.Money;


            object obj = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);

            if (obj == null)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        public DataSet GetListWhere(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select *");
            strSql.Append(" FROM Worker");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(ZG_member.Model.Worker model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Worker set ");
            strSql.Append("WorkerId=@WorkerId,");
            strSql.Append("Name=@Name,");
            strSql.Append("Sex=@Sex,");
            strSql.Append("BirthDate=@BirthDate,");
            strSql.Append("Money=@Money");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@WorkerId", SqlDbType.VarChar,50),
					new SqlParameter("@Name", SqlDbType.VarChar,200),
					new SqlParameter("@Sex", SqlDbType.VarChar,200),
					new SqlParameter("@BirthDate", SqlDbType.DateTime),
                    new SqlParameter("@Money", SqlDbType.Money,200),
					new SqlParameter("@Id", SqlDbType.Int,4)};
            parameters[0].Value = model.WorkerId;
            parameters[1].Value = model.Name;
            parameters[2].Value = model.Sex;
            parameters[3].Value = model.BirthDate;
            parameters[4].Value = model.Money;
            parameters[5].Value = model.Id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            return DbHelperSQL.Query(strWhere.ToString());
        }


        public bool Delete(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Worker ");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
};
            parameters[0].Value = id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        public ZG_member.Model.Worker GetModel(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 Id,WorkerId,Name,Sex,BirthDate,Money from Worker ");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
};
            parameters[0].Value = id;

            ZG_member.Model.Worker model = new ZG_member.Model.Worker();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["Id"] != null && ds.Tables[0].Rows[0]["Id"].ToString() != "")
                {
                    model.Id = int.Parse(ds.Tables[0].Rows[0]["Id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["WorkerId"] != null && ds.Tables[0].Rows[0]["WorkerId"].ToString() != "")
                {
                    model.WorkerId = ds.Tables[0].Rows[0]["WorkerId"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Name"] != null && ds.Tables[0].Rows[0]["Name"].ToString() != "")
                {
                    model.Name = ds.Tables[0].Rows[0]["Name"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Sex"] != null && ds.Tables[0].Rows[0]["Sex"].ToString() != "")
                {
                    model.Sex = ds.Tables[0].Rows[0]["Sex"].ToString();
                }
                if (ds.Tables[0].Rows[0]["BirthDate"] != null && ds.Tables[0].Rows[0]["BirthDate"].ToString() != "")
                {
                    model.BirthDate = Convert.ToDateTime(ds.Tables[0].Rows[0]["BirthDate"]);
                }
                if (ds.Tables[0].Rows[0]["Money"] != null && ds.Tables[0].Rows[0]["Money"].ToString() != "")
                {
                    model.Money = Convert.ToDouble(ds.Tables[0].Rows[0]["Money"]);
                }
                return model;
            }
            else
            {
                return null;
            }
        }
    }
}
